<?php

class FloorController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

        public function actionUpdate($floorNumber = null, $buildingId = null, $imageUrl = null, $id = null) {
            $model;
            if($id != null) {
                $model = Floor::model()->findByPk($id);
                if($model === null) {
                    $this->renderPartial("..\admin\jsonError", array("errId" => 3, "errEx" => "\"Requested record doesn't exists!\""));
                    Yii::app()->end();
                }
            } else {
                $model = new Floor();
            }

            //save
            if($floorNumber != null)    $model->floor_number = $floorNumber;
            if($buildingId != null)     $model->building_id = $buildingId;
            if($imageUrl != null)       $model->image_url = $imageUrl;
            try {
                if($model->save()) {
                    //Send OK JSON
                    $this->renderPartial("..\admin\jsonOK");
                } else {
                    //Send error JSON, save failed
                    $this->renderPartial("..\admin\jsonError", array("errId" => 2, "errEx" => "\"Make sure the building exists and floor number isn't taken!\""));
                }
            } catch(CDbException $e) {
                $this->renderPartial("..\admin\jsonError", array("errId" => 1, "errEx" => "\"Make sure the building exists and floor number isn't taken!\""));
            }
        }

        public function actionDelete($id) {
            Floor::model()->deleteByPk($id);
            $this->renderPartial("..\admin\jsonOK");
        }

        /**
         * Set image of specific id provided in post
         */
        public function actionSetImage() {
            if(!isset($_POST['floorId'])) {
                echo '{ "errEx" : "Invalid request!" }';
                Yii::app()->end();
            }

            //Find the model, make sure it exists
            $model = Floor::model()->findByPk($_POST['floorId']);
            if($model === null) {
                //Send JSON Error
                echo '{ "errEx" : "Floor with specified id is not found!" }';
                Yii::app()->end();
            }

            //Check uploaded file,
            if($_FILES['Filedata']['error'] != 0) {
                //Send JSON error, data crrupted
                echo '{ "errEx" : "Image file corrupted!" }';
            } else {
                $filename = microtime() . $_FILES['Filedata']['name'];
                $dir = Yii::getPathOfAlias("webroot.mapmodule.images").'/';

                if(!move_uploaded_file($_FILES['Filedata']['tmp_name'], $dir.$filename)) {
                    //Send JSON error, moving data failed
                    echo '{ "errEx" : "Internal error occured, please try again" }';
                } else {
                    //Moving succeeded, delete old image
                    if($model->image_url !== null) {
                        unlink($dir.$model->image_url);
                    }

                    //Assign new image
                    $model->image_url = $filename;
                    if(!$model->save()) {
                        //Error, failed to save
                        echo '{ "errEx" : "Internal error occured, please try again" }';
                    } else {
                        //Success
                        echo '{ "ok" : 1 }';
                    }
                }
            }
        }

        public function actionGetInfo() {
            $floorId = $_POST["floorId"];
            $model = Floor::model()->findByPk($floorId);
            if($model === null) {
                $models = Floor::model()->findAll();
                $model = $models[0];
            }
            $this->renderPartial("info", array("model" => $model));
        }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}